package com.huika.renrench.control.my.bank.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.huika.renrench.R;
import com.huika.renrench.control.common.base.BaseAda;
import com.huika.renrench.support.bean.BankCardBean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 
 * @description：我的银行卡适配器
 * @author zc
 * @date 2015年6月2日 上午11:16:06
 */
public class MyBankCardAdapter extends BaseAda<BankCardBean> {

	private DisplayImageOptions options;
	private SparseArray<View> map = new SparseArray<View>();

	public MyBankCardAdapter(Context context) {
		super(context);
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.icon_default_small)
		// resource or drawable
				.showImageForEmptyUri(R.drawable.icon_default_small).showImageOnFail(R.drawable.icon_default_small).cacheInMemory(true)// 开启内存缓存
				.cacheOnDisk(true) // 开启硬盘缓存
				.resetViewBeforeLoading(true).build();
	}

	private static class ViewHolder {
		ImageView img;
		TextView name;
		TextView bank_name;
		TextView last_no;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (map.get(position) == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_bank_card, null);
			holder.img = (ImageView) convertView.findViewById(R.id.img);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.bank_name = (TextView) convertView.findViewById(R.id.bank_name);
			holder.last_no = (TextView) convertView.findViewById(R.id.last_no);
			convertView.setTag(holder);
			final int p = position;
			map.put(p, convertView);
		}
		else {
			convertView = map.get(position);
			holder = (ViewHolder) convertView.getTag();
		}
		BankCardBean bean = group.get(position);
		ImageLoader.getInstance().displayImage(bean.getLogoImg(), holder.img, options);
		holder.name.setText(getHideRealName(bean.getRealName()));
		holder.bank_name.setText(bean.getBankName());
		holder.last_no.setText("尾号" + bean.getCardNumber().substring(bean.getCardNumber().length() - 4));
		return convertView;
	}

	private String getHideRealName(String realName) {
		StringBuffer sb = new StringBuffer(realName.substring(0, 1));
		int count = realName.length() - 1;
		for(int i = 0; i < count; i++) {
			sb.append('*');
		}
		return sb.toString();
	}
}
